<?php
namespace Qwadmin\Model;
use Think\Model;

class NovelModel extends Model{
	public function __construct() {
		parent::__construct();
	}
	
	/**
	 * 求书网抓取,
	 * 免费网站
	 * */
	public function getQiushuClassList(){
		//echo $class_content;die;
		$class_list = S("class_list");
		if(empty($class_list)){
			$base_url = "http://www.qiushu.cc";
			
			//伪造IP
			$header = fakeIp();
			//获得求书网所有分类
			$content = getUrlContent($base_url, $header);
			$begin_index = strpos($content, '<p class="hot_tips">') + strlen('<p class="hot_tips">');
			$end_index = strpos($content, "</p>", $begin_index);
			//echo $begin_index." ".$end_index;
			$class_content = substr($content, $begin_index, $end_index-$begin_index);
			$class_index = 1;
			while(1){
				if(strlen($class_content) > 0){
					//获得分类地址
					$url_begin = strpos($class_content, '<a href="') + strlen('<a href="');
					$url_end = strpos($class_content, '"', $url_begin);
					$class_url = $base_url.substr($class_content, $url_begin, $url_end - $url_begin);
					//获得分类名称
					$name_begin = strpos($class_content, '>') + 1;
					$name_end = strpos($class_content, '<', $name_begin);
					$class_name = substr($class_content, $name_begin, $name_end- $name_begin);
					$class_name = mb_convert_encoding($class_name, 'UTF-8','gbk');
					//加入数组
					$class_list[] = array("url"=>$class_url,"name"=>$class_name, "class_index"=>$class_index);
					$class_index++;
					
					//消减内容
					$cut_begin = strpos($class_content, '<');
					$cut_end = strpos($class_content, '>', $name_end) + 3;
					$class_content= substr($class_content, $cut_end, strlen($content) - $cut_end);
					//判断这些类型是否存在于我们的数据库中，没有就增加类型
					$c_count = D("novel_class")->where("class_name like '%".$class_name."%'")->count();
					if(empty($c_count))
						D("novel_class")->add(array("class_name"=>$class_name,"is_other"=>0));				
				}
				else 
					break;
			}
			//echo $content;
			S("class_list",$class_list);
			//print_r(S("class_list"));		
		}
		
		return $class_list;
	}
	
	/**
	 * 求书网
	 * 获得一种类型小说的总页数
	 * */
	public function getQiuShuNovelPages($url){
		//伪造IP
		$header = fakeIp();
		//获得求书网所有分类
		$content = getUrlContent($url, $header);
		
		$sub_start = strpos($content, '<em id="pagestats">') + "2" + strlen('<em id="pagestats">');
		$sub_end = strpos($content, '<', $sub_start) ;
		$page_count = substr($content, $sub_start, $sub_end - $sub_start);
		
		return $page_count;
	}
	
	/**
	 * 求书网
	 * 获得小说的内容
	 * */
	public function getQiuShuInfo($class_index = 0, $class_page = 0, $class_name = ''){
		set_time_limit(300);
		//echo $class_index;die;
		//获得全部类型
		$class_list = $this->getQiushuClassList();
		//print_r($class_list);
		if(empty($class_index)) {			
			foreach($class_list as $class_key=>$class_item){
				//获得这个类型
				//die("no class_index error stop");
				return 40001;
			}
		}
		else{
			$all_data = array();
			foreach($class_list as $class_key=>$class_item){
				//echo $class_index." ".$class_item["class_index"]."<br>";
				if(intval($class_index) == intval($class_item["class_index"])){
					if(!empty($class_page)){
						//伪造IP
						$header = fakeIp();
						//获得求书网所有分类
						$new_url = substr($class_item["url"], 0, strlen($class_item["url"]) - 6).$class_page.".html";
						$content = getUrlContent($new_url, $header);
						
						//抓取逻辑
						//echo $content;
						$read_start = strpos($content, '<ul class="read_list">')+strlen('<ul class="read_list">');
						$read_end = strpos($content, '</ul', $read_start);
						$read_list = substr($content, $read_start, $read_end - $read_start);
						//echo $read_list;
						while(strlen($read_list) > 10){
							//小说名称
							$sign1 = mb_convert_encoding("《", 'gbk', 'UTF-8');
							$sign2 = mb_convert_encoding("》", 'gbk', 'UTF-8');
							$sign3 = mb_convert_encoding("最新章节", 'gbk', 'UTF-8');
							$name_start = strpos($read_list, 'class="c1">') + strlen('class="c1">');
							$name_end = strpos($read_list, "<", $name_start);
							$name = substr($read_list, $name_start, $name_end- $name_start);
							$name = str_replace($sign1, "", $name);
							$name = str_replace($sign2, "", $name);
							$name = str_replace($sign3, "", $name);
							$data["name"] = mb_convert_encoding($name, 'UTF-8', 'gbk');
							//$name = mb_convert_encoding($name, 'gbk', 'UTF-8');
							//小说是否已经存在了
							//echo $name."<br>";
							$count = D("novel")->where(array("name"=>mb_convert_encoding($name, 'UTF-8', 'gbk')))->count();
							if($count > 0){
								$cell_end = strpos($read_list, "</li>") + 5;
								$read_list = substr($read_list, $cell_end, strlen($read_list) - $cell_end);
								//echo $read_list;
								continue;
							}
// 							else{
// 								echo mb_convert_encoding($data["name"], 'gbk', 'UTF-8');
// 								die;
// 							}
							//echo $count."<br>";
							//echo D()->getLastSql();							
							
							//小说介绍
							$des_start = strpos($read_list, '<span class="t3">') + strlen('<span class="t3">');
							$des_end = strpos($read_list, '<', $des_start);
							$description = substr($read_list, $des_start, $des_end- $des_start);
							$data["description"] = mb_convert_encoding($description, 'UTF-8', 'gbk');
							//echo $description;
							
							//图片
							$img_start = strpos($read_list, 'src="') + strlen('src="');
							$img_end = strpos($read_list, '"', $img_start);
							$img_url = substr($read_list, $img_start, $img_end- $img_start);
							
							//下载图片
							$img_info = getWebImg($img_url);
							$data["img"] = $img_info["save_path"].$img_info["file_name"];
							$data["mini_img"] = $data["img"];
							//echo $img;
							
							//查询是哪个分类
							$class = D("novel_class")->where("class_name = '$class_name'")->find();
							$data["class_id"] = $class["class_id"];
							$data["class_name"] = $class["class_name"];
							$data["other_class_id"] = 3;
							$data["other_class_name"] = "男生";
							$data["publish_time"] = date("Y-m-d H:i:s");
							
							//作者
							$w_span_start = strpos($read_list, '<span class="t2">') + strlen('<span class="t2">');
							$w_a_start = strpos($read_list, '>', $w_span_start) + 1;
							$w_a_end = strpos($read_list, '<', $w_a_start );
							$writer = substr($read_list, $w_a_start, $w_a_end- $w_a_start);
							$data["writer"] = mb_convert_encoding($writer, 'UTF-8', 'gbk');
							//echo $data["writer"];
							
							//来源网站
							$data["from"] = "求书网www.qiushu.cc";
							
							//采集小说的地址，以后便于更新
							$u_span_start = strpos($read_list, '<span class="t1">') + strlen('<span class="t1">');
							$href_start = strpos($read_list, '"', $u_span_start) + 1;
							$href_end = strpos($read_list, '"', $href_start);
							$data["web_from"] = substr($read_list, $href_start, $href_end- $href_start);
							//echo $data["web_from"];
							
							//采集是否完结
							$u_span_end = strpos($read_list, '</span>', $u_span_start);
							$u_content = substr($read_list, $u_span_start, $u_span_end- $u_span_start);
							$end_sign = mb_convert_encoding("连载中", 'gbk', 'UTF-8');
							$state = strpos($u_content, $end_sign);
							if($state > 1)
								$data["is_end"] = 0;
							else 
								$data["is_end"] = 1;
							
							//循环完结
							//删除数据
							$cell_end = strpos($read_list, "</li>") + 5;
							$read_list = substr($read_list, $cell_end, strlen($read_list) - $cell_end);
							
							//添加如数据库
							//D("novel")->add($data);
							$all_data[] = $data;
							
						}
						
						//die("over");
						D("novel")->addAll($all_data);
						
						//返回成功
						return 1;
					}
					else{
						//die("no class_page error stop");
						return 40002;
					}
				}
			}
		}
	}
}